package com.zqxq.auth.web;

import com.zqxq.auth.cache.UserFuncCache;
import com.zqxq.auth.entity.po.UserAdmin;
import com.zqxq.auth.entity.module.Code;
import com.zqxq.auth.entity.module.Result;
import com.zqxq.auth.service.IUserAdminService;
import com.zqxq.auth.tools.RedisTemplateUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * <p>
 * 管理员账号表 前端控制器
 * </p>
 *
 * @author 江枫
 * @since 2018-01-08
 */
@RestController
@RequestMapping(value = "/userAdmin", produces = "application/json")
public class UserAdminController {
    private static Logger logger = LoggerFactory.getLogger(UserAdminController.class);
    @Autowired
    private IUserAdminService userAdminService;
    @Autowired
    private RedisTemplateUtils redisTemplateUtils;
    @Autowired
    private UserFuncCache userFuncCache;

    @PostMapping("/test")
    @ApiOperation(value="用户密码登录", notes="用户密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "login_pwd", value = "登录密码", required = true, dataType = "String")
    })
    public Result test(String phone, String login_pwd){
        UserAdmin userAdmin = new UserAdmin();
        userAdmin.setAdduser(0l);
        userAdmin.setPhone(phone);
        userAdmin.setUser_name(System.currentTimeMillis()+"");
//        userAdminService.insertOrUpdate(userAdmin);
        return null;
    }
    @PostMapping("/test1")
    public String test1(String phone, String hk){
        Map map = new HashMap();
//        Random rd = new Random();
//        for(int i=0; i<10; i++) {
//            map.put(rd.nextInt(100000) + "", "");
//        }
//        map.put(hk, phone);
//        redisTemplateUtils.hashAddAll(phone, map, -1);
        String re = redisTemplateUtils.getHashValue(phone, hk);
//        if (re == null) {
//            System.out.println("空");
//        }
        System.out.println(redisTemplateUtils.containsKey(phone));
        return re;
    }

    /**
     * 管理员用户密码登录
     * @param phone
     * @param pwd
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value="用户密码登录", notes="用户密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "登录密码", required = true, dataType = "String", paramType = "query")
    })
    public Result login(String phone, String pwd){
        if (phone == null || pwd == null) {
            return new Result(Code.FAIL, "手机号或密码错误", Code.IS_ALERT_YES);
        }
        return userAdminService.login(phone, pwd);
    }

    @PostMapping("/loginOut")
    @ApiOperation(value="用户注销", notes="用户注销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token_id", value = "token id", required = true, dataType = "String", paramType = "query")
    })
    public Result loginOut(String token_id) {
        return userAdminService.loginOut(token_id);
    }

    @PostMapping("/updatePwd")
    @ApiOperation(value="修改密码", notes="修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token_id", value = "token id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "old_pwd", value = "旧密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "new_pwd", value = "新密码", required = true, dataType = "String", paramType = "query")
    })
    public Result updatePwd(String token_id, String old_pwd, String new_pwd) {
        return userAdminService.updatePwd(token_id, old_pwd, new_pwd);
    }

    @PostMapping("/getUserList")
    @ApiOperation(value="获取用户列表", notes="获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数，以1开始", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "PageSize", value = "页面显示长度", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getUserList(HttpServletRequest request, Integer pageNo, Integer pageSize) {
        Long role_id = userFuncCache.getUserAdmin(request).getRole_id();
        return userAdminService.getUserList(role_id, pageNo, pageSize);
    }
    @PostMapping("/updateUserStatus")
    @ApiOperation(value="修改用户状态", notes="修改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "stop", value = "信息状态:1停用,0正常使用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result updateUserStatus(Long user_id, Integer stop) {
        if (stop == null || stop > 1 || stop < 0) {
            return new Result(Code.FAIL, "信息状态非法");
        }
        UserAdmin userAdmin = new UserAdmin();
        userAdmin.setUser_id(user_id);
        userAdmin.setStop(stop);
        userAdminService.updateById(userAdmin);
        return new Result();
    }

    @ApiOperation(value = "保存用户",notes = "保存用户")
    @PostMapping("/saveUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "账号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "user_name", value = "用户名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "stop", value = "信息状态:1停用,0正常使用", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "role_id", value = "角色id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "phone_auth", value = "手机号查看权限：0=脱敏，1=不脱敏", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id_no_auth", value = "手机号查看权限：0=脱敏，1=不脱敏", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "bank_auth", value = "银行卡查看权限：0=脱敏，1=不脱敏", dataType = "Integer", paramType = "query")
    })
    public Result saveUser(UserAdmin userAdmin,  HttpServletRequest request) {
        logger.info("saveUser入参：role={}", userAdmin.toString());
        return userAdminService.saveUser(userAdmin, userFuncCache.getUserAdmin(request));
    }

    @ApiOperation(value = "验证登录名是否通过",notes = "验证登录名是否通过，data=true 通过")
    @PostMapping("/isPhonePass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "账号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "user_id", value = "用户id", dataType = "Long", paramType = "query")

    })
    public Result isPhonePass(String phone, Long user_id) {
        return userAdminService.isPhonePass(phone, user_id);
    }

    @ApiOperation(value = "根据用户id获取用户信息",notes = "根据用户id获取用户信息")
    @PostMapping("/getUserInfoByUserId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id", dataType = "Long", paramType = "query")

    })
    public Result getUserInfoByUserId(Long user_id) {
        return userAdminService.getUserInfoByUserId(user_id);
    }
}
